﻿namespace crm_utility.Pager
{
    /// <summary>
    /// 分页模型
    /// </summary>
    public class PageModel
    {
        /// <summary>
        /// 当前页索引
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 当前页数
        /// </summary>
        public int PageNumber { get; set; }
        /// <summary>
        /// 上一页数
        /// </summary>
        public int PrePageNumber { get; set; }
        /// <summary>
        /// 下一页数
        /// </summary>
        public int NextPageNumber { get; set; }
        /// <summary>
        /// 每页数
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总项数
        /// </summary>
        public int TotalCount { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages { get; set; }
        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPrePage { get; set; }
        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNextPage { get; set; }
        /// <summary>
        /// 是否是第一页
        /// </summary>
        public bool IsFirstPage { get; set; }
        /// <summary>
        /// 是否是最后一页
        /// </summary>
        public bool IsLastPage { get; set; }

        public PageModel(int pageNumber, int pageSize, int totalCount)
        {
            if (pageSize > 0)
                PageSize = pageSize;
            else
                PageSize = 1;

            if (pageNumber > 0)
                PageNumber = pageNumber;
            else
                PageNumber = 1;

            if (totalCount > 0)
                TotalCount = totalCount;
            else
                TotalCount = 0;

            PageIndex = pageNumber - 1;

            TotalPages = TotalCount / PageSize;
            if (TotalCount % PageSize > 0)
                TotalPages++;

            HasPrePage = pageNumber > 1;
            HasNextPage = PageNumber < TotalPages;

            IsFirstPage = PageNumber == 1;
            IsLastPage = PageNumber == TotalPages;

            PrePageNumber = PageNumber < 2 ? 1 : PageNumber - 1;
            NextPageNumber = PageNumber < TotalPages ? PageNumber + 1 : TotalPages;
        }
    }
}